<?php
class user{
	protected $id,$name,$username,$password,$email,$country,$role,$usercred,$createdAt,$updatedA,$sid;
	
	function __construct($id,$name,$username,$password,$email,$countryid,$roleid,$usercred,$createdAt,$updatedAt,$sid){
		$this->id = $id;
		$this->name = $name;
		$this->username = $username;
		$this->password = $password;
		$this->email = $email;
		$this->country = country::getCountryById($countryid);
		$this->role = role::getRoleById($roleid);
		$this->usercred = $usercred;
		$this->createdAt = $createdAt;
		$this->updatedAt = $updatedAt;
		$this->sid = $sid;
	}

	/*GETTTERS*/
	public function getId(){
		return $this->id;	
	}

	public function getName(){
		return $this->name;
	}

	public function getUsername(){
		return $this->username;
	}
	
	public function getPassword(){
		return $this->password;
	}

	public function getEmail(){
		return $this->email;	
	}
	
	public function getCountry(){
		return $this->country;	
	}

	public function getRole(){
		return $this->role;	
	}
	
	public function getUserCredibility(){
		return $this->usercred;	
	}	
	
	public function getCreatedAt(){
		return $this->createdAt;	
	}
	
	public function getUpdatedAt(){
		return $this->updatedAt;	
	}
	
	public function getSid(){
		return $this->sid;
	}

	/*SETTERS*/
	public function setName($var){
		$this->name = $var;
	}

	public function setUsername($var){
		$this->username = $var;
	}

	public function setPassword($var){
		$this->password = $var;
	}

	public function setEmail($var){
		$this->email = $var;
	}
	
	public function setCountry($var){
		$this->country = $var;
	}
	
	public function setRole($var){
		$this->role = $var;
	}

	public function setUserCredibility($var){
		$this->usercred = $var;	
	}	
	
	public function setSid($var){
		$this->sid = $var;
	}

	public function toString(){
		echo "<br>";
		echo "ID: ".$this->id."<br>";
		echo "Name: ".$this->name."<br>";
		echo "Username: ".$this->username."<br>";
		echo "Password: ".$this->password."<br>";
		echo "Email: ".$this->email."<br>";
		echo "Country: ".$this->country->getName()."<br>";
		echo "Role: ".$this->role->getName()."<br>";
		echo "Created At: ".date('y-m-d h:i:s',$this->createdAt)."<br>";
		echo "Updated At: ".date('y-m-d h:i:s',$this->updatedAt)."<br>";
	}

	public static function getUserById($id){
		$conn = new dbmodifier();
		$id = $conn->db_connection->real_escape_string($id);
		$query = "SELECT name, username, password, email, country_id,".
					" role_id,credibility, UNIX_TIMESTAMP(created_at) AS 'created_at',".
					"UNIX_TIMESTAMP(updated_at) AS 'updated_at', sid FROM USER WHERE id =".$id."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();
			$var = new user($id,$row['name'],$row['username'],$row['password']
					,$row['email'],$row['country_id'],$row['role_id'],$row['credibility']
					,$row['created_at'],$row['udpated_at'],$row['sid']);
		}else{
			$var = NULL;
		}
		return $var;		
	}

	public static function getUserByUsernameAndPassword($username,$password){
		$conn = new dbmodifier();
		$username = $conn->db_connection->real_escape_string($username);
		$password = $conn->db_connection->real_escape_string($password);		
		$query = "SELECT id, name, username, password, email, country_id,".
					" role_id, credibility, UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at', sid FROM USER WHERE username ='".$username.
					"' AND password='".$password."'";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();
			$var = new user($row['id'],$row['name'],$row['username'],$row['password']
					,$row['email'],$row['country_id'],$row['role_id'],$row['credibility']
					,$row['created_at'],$row['updated_at'],$row['sid']);
		}else{
			$var = NULL;
		}
		return $var;		
	}
	
	public static function getUserBySid($sid){
		$conn = new dbmodifier();
		$sid = $conn->db_connection->real_escape_string($sid);		
		$query = "SELECT id FROM USER WHERE sid ='".$sid."'";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$var = 1;
		}else{
			$var = -1;
		}
		return $var;		
	}
	
	
	public static function createUser($name,$username,$password,$email,$countryid,$roleid,$credibility){
		$conn = new dbmodifier();
		$name = $conn->db_connection->real_escape_string($name);
		$username = $conn->db_connection->real_escape_string($username);
		$password = $conn->db_connection->real_escape_string($password);
		$email = $conn->db_connection->real_escape_string($email);
		$countryid = $conn->db_connection->real_escape_string($countryid);
		$roleid = $conn->db_connection->real_escape_string($roleid);
		$credibility = $conn->db_connection->real_escape_string($credibility);		
		$query = "INSERT INTO USER (name,username,password,email,country_id,role_id,credibility,created_at,updated_at) " .
				 " VALUES ('".$name."','".$username."','".
				 $password."','".$email."','".$countryid."','".
				 $roleid."','".$credibility."',FROM_UNIXTIME(".time()."),FROM_UNIXTIME(".time()."))";
		$id = $conn->updateSQLKey($query);
		$conn->execute();
		$var = new user($id,$name,$username,$password,$email,$countryid,$roleid,$credibility,time(),time());
		return $var;	
	}
	
	public static function getAllUserByType($var){
		$conn = new dbmodifier();
		$query = "SELECT * FROM USER WHERE role_id=";
		$role = role::getRoleByName($var);
		$query = $query . $role->getId();
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;
	}
	
	public static function checkUserExistance($username){
		$conn = new dbmodifier();
		$query = "SELECT id FROM USER WHERE username ='".$username."'";
		$result = $conn->updateSQL($query);	
		$conn->execute();
		if(($result->num_rows)!=0){
			return 0;
		}else{
			return -1;
		}
	}

	public function save(){
		$conn = new dbmodifier();
		$name = $conn->db_connection->real_escape_string($this->name);
		$username = $conn->db_connection->real_escape_string($this->username);
		$password = $conn->db_connection->real_escape_string($this->password);
		$email = $conn->db_connection->real_escape_string($this->email);
		$countryid = $conn->db_connection->real_escape_string($this->country->getId());
		$roleid = $conn->db_connection->real_escape_string($this->role->getId());
		$credibility = $conn->db_connection->real_escape_string($this->credibility);
		$sid = $conn->db_connection->real_escape_string($this->sid);		
		$query = "UPDATE USER SET name='".$name."'";
		$query = $query.", username='".$username."'";
		$query = $query.", email='".$email."'";
		$query = $query.", country_id='".$countryid."'";
		$query = $query.", role_id='".$roleid."'";
		$query = $query.", credibility='".$credibility."'";		
		$query = $query.", updated_at= FROM_UNIXTIME(".time().")";		
		$query = $query.", sid= '".$sid."'";		
		$query = $query." WHERE id=".$this->id;
		$conn->updateSQL($query);
		$conn->execute();	
		$this->updatedAt = time();
	}	
	
	public function delete(){
		$conn = new dbmodifier();
		$query = "DELETE FROM USER WHERE id=".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;
	}
}
?>
